package com.kingtone.rongji;

import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import cn.sharesdk.framework.ShareSDK;

import com.baidu.mapapi.SDKInitializer;
import com.kingtone.rongji.tool.RongJiUtils;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

public class RongJiApplication extends Application {

	private List<Activity> activityList = new LinkedList();

	private static RongJiApplication instance;

	public static RongJiApplication getInstance() {
		if (null == instance) {
			instance = new RongJiApplication();
		}
		return instance;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		ShareSDK.initSDK(this);
		SDKInitializer.initialize(getApplicationContext());
		initImageLoader(this);
		RongJiUtils.startLocation(this.getApplicationContext());
	}

	public void addActivity(Activity activity) {
		activityList.add(activity);
	}

	public void exitApp() {
		for (Activity activity : activityList) {
			activity.finish();
		}
		System.exit(0);
	}

	public static void initImageLoader(Context context) {
		ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(
				context);
		config.threadPriority(Thread.NORM_PRIORITY - 2);
		config.denyCacheImageMultipleSizesInMemory();
		config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
		config.diskCacheSize(5 * 1024 * 1024); // 50 MiB
		config.tasksProcessingOrder(QueueProcessingType.LIFO);
		config.discCacheFileCount(100);
		ImageLoader.getInstance().init(config.build());
	}
}
